package com.xu.t1.middleware.online;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时更新维护当前节点历史在线人数的组件类
 *
 * @author xu
 *
 */

@Component
public class OnlineUserHistoryUpdater {

    @Value("${app.server-id}")
    private String serverId;

    @Autowired
    private OnlineUserManager onlineUserManager;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Scheduled(fixedRate = 1000)
    public void updateOnlineUserHistory() {
        // 获取当前在线人数
        int serverOnlineCount = onlineUserManager.getServerOnlineCount(serverId);

        String key = "online:history:" + serverId;
        redisTemplate.opsForList().leftPush(key, String.valueOf(serverOnlineCount));
        redisTemplate.opsForList().trim(key, 0, 9999);
    }
}